<?php
namespace app\common\tool;

/**
 * 巨量引擎接口工具类
 */
class Oceanengine
{
    private const API_LIST = [
        'ACCESS_TOKEN'              => 'https://ad.oceanengine.com/open_api/oauth2/access_token/',                          //auth_code获取access_token
        'REFRESH_TOKEN'             => 'https://ad.oceanengine.com/open_api/oauth2/refresh_token/',                         //刷新access_token和refresh_token
        'AUTHED_LIST'               => 'https://ad.oceanengine.com/open_api/oauth2/advertiser/get/',                        //获取已经授权列表
        'AUTHED_USER'               => 'https://ad.oceanengine.com/open_api/2/user/info/',                                  //获取授权User信息
        'AGENT_ADVERTISER'          => 'https://ad.oceanengine.com/open_api/2/agent/advertiser/select/',                    //获取代理商管理账户列表
        'CUSTOMER_ADVERTISER'       => 'https://ad.oceanengine.com/open_api/2/customer_center/advertiser/list/',            //获取纵横组织下资产账户列表
        'ADVERTISER_INFO'           => 'https://ad.oceanengine.com/open_api/2/advertiser/info/',                            //获取广告主信息
        'ADVERTISER_PUBLIC_INFO'    => 'https://ad.oceanengine.com/open_api/2/advertiser/public_info/',                     //获取广告主公开信息
        'AD_REPORT_DATA'            => 'https://ad.oceanengine.com/open_api/2/report/advertiser/get/',                      //广告主数据
        'GET_AD_LIST_ALL'           => 'https://ad.oceanengine.com/open_api/2/majordomo/advertiser/select/',                //获取纵横组织下资产账户列表(全量)
        'GET_AD_PROJECT'            => 'https://api.oceanengine.com/open_api/v3.0/project/list/',                           //获取广告项目列表
        'GET_AD_PROMOTION'          => 'https://api.oceanengine.com/open_api/v3.0/promotion/list/',                         //获取广告列表
        'GET_AD_APP_INFO'           => 'https://ad.oceanengine.com/open_api/2/tools/app_management/app/get/',               //获取应用信息
        'GET_PRODUCT_INFO'          => 'https://ad.oceanengine.com/open_api/2/dpa/product/availables/',                     //获取商品库信息
        'GET_PRODUCT_LIST'          => 'https://ad.oceanengine.com/open_api/2/dpa/detail/get/',                             //获取商品列表
        'GET_AD_AUDIENCE_PACKAGE'   => 'https://ad.oceanengine.com/open_api/2/audience_package/get/',                       //获取广告主定向包
        'DEL_AD_AUDIENCE_PACKAGE'   => 'https://ad.oceanengine.com/open_api/2/audience_package/delete/',                    //删除广告主定向包
        'GET_AD_IMAGE'              => 'https://api.oceanengine.com/open_api/2/file/image/get/',                            //获取图片素材
        'GET_AD_VIDEO'              => 'https://api.oceanengine.com/open_api/2/file/video/get/',                            //获取视频素材
        'GET_OPTIMIZED_GOAL'        => 'https://api.oceanengine.com/open_api/v3.0/event_manager/optimized_goal/get_v2/',    //获取可用优化目标(巨量广告升级版)
        'GET_DEEP_BID_TYPE'         => 'https://api.oceanengine.com/open_api/v3.0/event_manager/deep_bid_type/get/',        //获取可用深度优化方式(广告投放升级版)
        'GET_AD_ASSETS'             => 'https://ad.oceanengine.com/open_api/2/tools/event/assets/get/',                     //获取已创建资产列表
        'GET_SUGG_WORDS'            => 'https://api.oceanengine.com/open_api/v3.0/sugg_words/',                             //获取推荐关键词
        'GET_SEARCH_BID_RATIO'      => 'https://api.oceanengine.com/open_api/2/tools/search_bid_ratio/get/',                //获取快投推荐出价系数
        'GET_AWEME_AUTH_LIST'       => 'https://api.oceanengine.com/open_api/2/tools/aweme_auth_list/',                     //获取抖音授权关系
        'GET_CREATIVE_COMPONENT'    => 'https://ad.oceanengine.com/open_api/2/assets/creative_component/get/',              //获取组件列表
        'GET_AD_ORANGE_SITE'        => 'https://api.oceanengine.com/open_api/v3.0/tools/orange_site/get/',                  //通过优化目标获取橙子落地页站点信息
        'GET_THIRD_SITE'            => 'https://ad.oceanengine.com/open_api/2/tools/third_site/get/',                       //获取第三方落地页站点列表
        'GET_IMAGE_MATERIAL'        => 'https://api.oceanengine.com/open_api/2/file/image/get/',                            //获取图片素材
        'GET_VIDEO_MATERIAL'        => 'https://api.oceanengine.com/open_api/2/file/video/get/',                            //获取视频素材
        'GET_AD_BRAND_INFO'         => 'https://api.oceanengine.com/open_api/v3.0/cdp/brand/get/',                          //获取关联云图的广告主账户信息
        'GET_RECOMMEND_TITLE'       => 'https://ad.oceanengine.com/open_api/2/tools/promotion_card/recommend_title/get/',   //查询推广卡片推荐内容(新版)
        'GET_ADVERTISER_REPORT'     => 'https://ad.oceanengine.com/open_api/2/report/advertiser/get/',                      //获取广告主数据
        'GET_QIANCHUAN_AD_INFO'     => 'https://ad.oceanengine.com/open_api/2/advertiser/public_info/',           //获取千川广告主数据
    ];

    // 广告主消耗字段数组
    private $ad_report_fields = 
        [
            'cost','show','avg_show_cost','click','avg_click_cost','ctr','convert','convert_cost','convert_rate','deep_convert','deep_convert_cost',
            'deep_convert_rate','attribution_convert','attribution_convert_cost','attribution_deep_convert','attribution_deep_convert_cost',
            'download_start','download_start_cost','download_start_rate','download_finish','download_finish_cost','download_finish_rate','click_install',
            'install_finish','install_finish_cost','install_finish_rate','active','active_cost','active_rate','register','active_register_cost',
            'active_register_rate','attribution_next_day_open_cnt','attribution_next_day_open_cost','attribution_next_day_open_rate',
            'attribution_retention_2d_cnt','attribution_retention_2d_cost','attribution_retention_2d_rate','attribution_retention_3d_cnt',
            'attribution_retention_3d_cost','attribution_retention_3d_rate','attribution_retention_4d_cnt','attribution_retention_4d_cost',
            'attribution_retention_4d_rate','attribution_retention_5d_cnt','attribution_retention_5d_cost','attribution_retention_5d_rate',
            'attribution_retention_6d_cnt','attribution_retention_6d_cost','attribution_retention_6d_rate','attribution_retention_7d_cnt',
            'attribution_retention_7d_cost','attribution_retention_7d_rate','attribution_retention_7d_sum_cnt','attribution_retention_7d_total_cost',
            'game_addiction','game_addiction_cost','game_addiction_rate','pay_count','active_pay_cost','active_pay_rate','in_app_uv','in_app_detail_uv',
            'in_app_cart','in_app_pay','in_app_order','attribution_game_pay_7d_count','attribution_game_pay_7d_cost','attribution_active_pay_7d_per_count',
            'game_pay_cost','game_pay_count','stat_pay_amount','pay_amount_roi','phone','form','map_search','button','view','download','qq','lottery','vote',
            'message','redirect','shopping','consult','wechat','phone_confirm','phone_connect','consult_effective','coupon','coupon_single_page',
            'redirect_to_shop','poi_collect','poi_address_click','customer_effective','wechat','advanced_creative_phone_click','advanced_creative_counsel_click',
            'advanced_creative_form_click','advanced_creative_coupon_addition','advanced_creative_form_submit','phone_effective','attribution_micro_game_0d_ltv',
            'attribution_micro_game_3d_ltv','attribution_micro_game_7d_ltv','attribution_micro_game_0d_roi','attribution_micro_game_3d_roi',
            'attribution_micro_game_7d_roi','stat_union_ltv_0','stat_union_ltv_3','stat_union_ltv_7','union_roi_0','union_roi_3','union_roi_7','total_play','valid_play',
            'valid_play_cost','valid_play_rate','play_25_feed_break','play_50_feed_break','play_75_feed_break','play_100_feed_break','average_play_time_per_play',
            'play_over_rate','wifi_play_rate','wifi_play','play_duration_sum','card_show','share','comment','like','follow','ies_challenge_click','ies_music_click',
            'location_click','home_visited','message_action','click_landing_page','click_shopwindow','click_website','click_download','click_call_dy','luban_live_enter_cnt',
            'live_watch_one_minute_count','luban_live_follow_cnt','live_fans_club_join_cnt','luban_live_comment_cnt','luban_live_share_cnt','luban_live_gift_cnt',
            'luban_live_gift_amount','luban_live_slidecart_click_cnt','luban_live_click_product_cnt','luban_live_pay_order_count','luban_live_pay_order_stat_cost',
            'live_component_click_count','live_component_click_cost','live_component_click_rate','attribution_active_pay_intra_one_day_count',
            'attribution_active_pay_intra_one_day_cost','attribution_active_pay_intra_one_day_rate','attribution_active_pay_intra_one_day_amount',
            'attribution_active_pay_intra_one_day_roi','attribution_game_in_app_ltv_1day','attribution_game_in_app_ltv_5days','attribution_game_in_app_ltv_7days',
            'attribution_game_in_app_roi_1day','attribution_game_in_app_roi_3days','attribution_game_in_app_roi_5days','attribution_game_in_app_roi_7days',
            'attribution_active_pay_7d_count','attribution_active_pay_7d_rate','attribution_game_in_app_ltv_2days','attribution_game_in_app_ltv_4days',
            'attribution_game_in_app_ltv_6days','attribution_game_in_app_ltv_8days','attribution_game_in_app_roi_2days','attribution_game_in_app_roi_4days',
            'attribution_game_in_app_roi_6days','attribution_game_in_app_roi_8days','attribution_active_pay_7d_cost','attribution_day_active_pay_count','loan_completion',
            'loan_completion_cost','loan_completion_rate','pre_loan_credit','pre_loan_credit_cost','loan_credit','loan_credit_cost','loan_credit_rate','wechat_login_count',
            'attribution_wechat_login_30d_count','wechat_login_cost','attribution_wechat_login_30d_cost','wechat_first_pay_count','attribution_wechat_first_pay_30d_count',
            'wechat_first_pay_cost','attribution_wechat_first_pay_30d_cost','wechat_first_pay_rate','attribution_wechat_first_pay_30d_rate','wechat_pay_amount',
            'attribution_wechat_pay_30d_amount','attribution_wechat_pay_30d_roi','submit_certification_count','approval_count','first_rental_order_count','first_order_count',
            'commute_first_pay_count','attribution_game_in_app_ltv_3days'
        ];

    private $common_header = [];

    private $common_params = [];

    private static $instance = null;
 
    // 禁止被实例化
    // 初始公共Header和公共参数
    private function __construct()
    {
        $this->common_params = [
            'app_id'   => Config('oceanengine.appid'),
            'secret'   => Config('oceanengine.appsecret'),
        ];
        
        $this->common_header = [
            "Content-Type: application/json",
            "X-Debug-Mode: 1"
        ];
    }
 
    // 禁止clone
    private function __clone()
    {
        
    }

    // 实例化自己并保存到$instance中，已实例化则直接调用
    public static function getInstance()
    {
        if (empty(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    // 获取api列表
    private function getApiUrl()
    {
        return self::API_LIST;
    }

    /**
     * 获取access_token
     * @param   string  $code
     */
    public function get_access_token($code)
    {
        $header = $this->common_header;
        $params = array_merge($this->common_params,[
            'grant_type' => 'auth_code',
            'auth_code'  => $code
        ]);
        $res = httpRequest($this->getApiUrl()['ACCESS_TOKEN'],json_encode($params),$header);
        return $res;
    }

    /**
     * 刷新access_token
     * @param   string  $access_token
     */
    public function get_refresh_token($refresh_token)
    {
        $header = $this->common_header;
        $params = array_merge($this->common_params,[
            'refresh_token' => $refresh_token,
        ]);
        $res = httpRequest($this->getApiUrl()['REFRESH_TOKEN'],json_encode($params),$header);
        return $res;    
    }

    /**
     * 获取已授权账户
     * @param   string  $access_token
     */
    public function get_authed_advertiser_list($access_token)
    {
        $params = array_merge($this->common_params,[
            'access_token'  =>  $access_token,
        ]);
        $header = $this->common_header;
        $url = $this->getApiUrl()['AUTHED_LIST'] . '?' . http_build_query($params);
        $res = httpRequest($url,[],$header);
        return $res;
    }

    /**
     * 获取授权User信息
     * @param   string  $access_token
     */
    public function get_authed_userinfo($access_token)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $url = $this->getApiUrl()['AUTHED_USER'];
        $res = httpRequest($url,[],$header);
        return $res;    
    }
    
    /**
     * 获取广告项目分页列表
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   string  $project_id
     * @param   int     $pagesize
     * @param   int     $page
     */
    public function get_project_pagelist($access_token, $advertiser_id, $project_id, $pagesize, $page)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id'     =>  $advertiser_id,
            // 'fields'            =>  json_encode([
            //     'project_id',
            //     'advertiser_id',
            //     'delivery_mode',
            //     'landing_type',
            //     'app_promotion_type',
            //     'marketing_goal',
            //     'ad_type',
            //     'opt_status',
            //     'name',
            //     'status',
            //     'status_first',
            //     'status_second',
            //     'pricing',
            //     'asset_type',
            //     'optimize_goal',
            //     'project_create_time',
            //     'delivery_setting',
            // ]),
            'page_size'         =>  $pagesize,
            'page'              =>  $page,
        ];
        if(!empty($project_id)){
            $params['filtering'] = json_encode([
                'ids'   => [$project_id]    
            ]);   
        }
        $url = $this->getApiUrl()['GET_AD_PROJECT'] . '?' . http_build_query($params);
        $res = httpRequestBigint($url,[],$header);
        return $res;
    }

    /**
     * 获取广告分页列表
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   string  $project_id
     * @param   string  $project_id
     * @param   string  $exact
     * @param   int     $pagesize
     * @param   int     $page
     */
    public function get_promotion_pagelist($access_token, $advertiser_id, $pagesize, $page, $project_id, $promotion_id, $exact)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id'     =>  $advertiser_id,
            'fields'            =>  json_encode([
                'promotion_id',
                'promotion_name',
                'project_id',
                'advertiser_id',
                'promotion_create_time',
                'budget',
                'budget_mode',
                'cpa_bid',
                'status',
                'status_first',
                'status_second',
                'opt_status',
                'promotion_materials',
            ]),
            'page_size'         =>  $pagesize,
            'page'              =>  $page,
        ];
        $filtering = [];
        if(!empty($project_id)){
            $filtering['project_id'] = $project_id;   
        }
        // if(!empty($promotion_id)){
        //     $filtering['ids'] = [$promotion_id]; 
        // }
        if(!empty($exact)){
            if(is_numeric($exact)){
                $filtering['ids'] = [$exact];    
            }else{
                $filtering['name'] = $exact;
            }
        }
        if(!empty($filtering)){
            $params['filtering'] = json_encode($filtering);   
        }
        $url = $this->getApiUrl()['GET_AD_PROMOTION'] . '?' . http_build_query($params);
        $res = httpRequestBigint($url,[],$header);
        return $res;
    }

    /**
     * 代理商管理账户列表(分页查询)
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   int     $pagesize
     * @param   int     $page
     */
    public function get_agent_advertiser_pagelist($access_token, $advertiser_id, $pagesize, $page)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id'     =>  $advertiser_id,
            'page_size'         =>  $pagesize,
            'page'              =>  $page,
        ];
        $url = $this->getApiUrl()['AGENT_ADVERTISER'] . '?' . http_build_query($params);
        $res = httpRequest($url,[],$header);
        return $res;
    }

    /**
     * 代理商管理账户列表(游标查询)
     */
    public function get_agent_advertiser_cursorlist($access_token, $advertiser_id, $pagesize, $cursor)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id'     =>  $advertiser_id,
            'cursor'            =>  $cursor,
            'count'             =>  $pagesize,
        ];
        $url = $this->getApiUrl()['AGENT_ADVERTISER'] . '?' . http_build_query($params);
        $res = httpRequest($url,[],$header);
        return $res;
    }

    /**
     * 获取纵横组织下资产账户列表(分页查询)
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   int     $pagesize
     * @param   int     $page
     */
    public function get_customer_center_advertiser_pagelist($access_token, $advertiser_id, $pagesize, $page)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'cc_account_id'     =>  $advertiser_id,
            'page_size'         =>  $pagesize,
            'page'              =>  $page,
        ];
        $url = $this->getApiUrl()['CUSTOMER_ADVERTISER'] . '?' . http_build_query($params);
        $res = httpRequest($url,[],$header);
        return $res;
    }

    /**
     * 获取广告主信息
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_info($access_token, $advertiser_id)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_ids'     =>  json_encode([
                $advertiser_id    
            ])
        ];
        $url = $this->getApiUrl()['ADVERTISER_INFO'] . '?' . http_build_query($params);
        $res = httpRequest($url,[],$header);
        return $res;
    }

    /**
     * 获取广告主公开信息
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_public_info($access_token, $advertiser_id)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_ids'     =>  json_encode([
                $advertiser_id    
            ])
        ];
        $url = $this->getApiUrl()['ADVERTISER_PUBLIC_INFO'] . '?' . http_build_query($params);
        $res = httpRequest($url,[],$header);
        return $res;
    }

    /**
     * 查询应用信息
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_app_info($access_token, $advertiser_id)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id'    =>  $advertiser_id
        ];
        $url = $this->getApiUrl()['GET_AD_APP_INFO'] . '?' . http_build_query($params);
        $res = httpRequest($url,[],$header);
        return $res;    
    }

    /**
     * 查询IOS应用信息
     * 请求apple官方接口获取    
     * @param   string  $appstore_id
     */
    public function get_ios_app_info($appstore_id)
    {
        $url = "https://itunes.apple.com/lookup?id=" . $appstore_id;
        $res = httpRequest($url,[],[]);
        return $res;      
    }

    /**
     * 获取商品库信息
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_product_info($access_token, $advertiser_id)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id'    =>  $advertiser_id
        ];
        $url = $this->getApiUrl()['GET_PRODUCT_INFO'] . '?' . http_build_query($params);
        $res = httpRequestBigint($url,[],$header);
        return $res;
    }


    /**
     * 获取商品列表信息,根据商品库id获取
     * @param   int     $page
     * @param   int     $pagesize
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   string  $product_platform_id 
     */
    public function get_advertiser_product_list($access_token, $advertiser_id, $product_platform_id, $pagesize, $page)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id'         =>  $advertiser_id,
            'product_platform_id'   =>  $product_platform_id,
        ];
        $url = $this->getApiUrl()['GET_PRODUCT_LIST'] . '?' . http_build_query($params);
        $res = httpRequestBigint($url,[],$header);
        return $res;
    }

    /**
     * 获取广告主下定向包
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   string  $landing_type
     * @param   string  $delivery_range
     * @param   string  $ad_type
     * @param   string  $marketing_goal
     * @param   int     $pagesize
     * @param   int     $page
     */
    public function get_advertiser_audience_package($access_token, $advertiser_id, $landing_type, $delivery_range, $ad_type, $marketing_goal, $pagesize, $page)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  $pagesize,
            'page'          =>  $page,
        ];
        $filtering = [];
        if(!empty($landing_type)){
            $filtering['landing_type']    = $landing_type;
        }
        if(!empty($delivery_range)){
            $filtering['delivery_range']  = $delivery_range;
        }
        if(!empty($ad_type)){
            $filtering['ad_type']         = $ad_type;
        }
        if(!empty($marketing_goal)){
            $filtering['marketing_goal']  = $marketing_goal;
        }
        if(!empty($filtering)){
            $params['filtering'] = json_encode($filtering);    
        }
        $url = $this->getApiUrl()['GET_AD_AUDIENCE_PACKAGE'] . '?' . http_build_query($params);
        $res = httpRequestBigint($url,[],$header);
        return $res;
    }

    /**
     * 删除定向包
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   string  $audience_package_id
     */
    public function delete_advertiser_audience_package($access_token, $advertiser_id, $audience_package_id)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id'         =>  (int)$advertiser_id,
            'audience_package_id'   =>  (int)$audience_package_id,
        ];
        $url = $this->getApiUrl()['DEL_AD_AUDIENCE_PACKAGE'];
        $res = httpRequest($url,json_encode($params),$header);
        return $res;
    }

    /**
     * 获取图片素材(分页)
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   int     $pagesize
     * @param   int     $page
     */
    public function get_advertiser_image($access_token, $advertiser_id, $pagesize, $page)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  $pagesize,
            'page'          =>  $page,
        ];
        $url = $this->getApiUrl()['GET_AD_IMAGE'] . '?' . http_build_query($params);
        $res = httpRequestBigint($url,[],$header);
        return $res;
    }

    /**
     * 获取视频素材(分页)
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   int     $pagesize
     * @param   int     $page
     */
    public function get_advertiser_video($access_token, $advertiser_id, $pagesize, $page)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $params = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  $pagesize,
            'page'          =>  $page,
        ];
        $url = $this->getApiUrl()['GET_AD_VIDEO'] . '?' . http_build_query($params);
        $res = httpRequestBigint($url,[],$header);
        return $res;
    }

    /**
     * 获取可用优化目标
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   array   $params
     */
    public function get_advertiser_optimized_goal($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id'     =>  $params['advertiser_id'],
            'landing_type'      =>  !empty($params['landing_type']) ? $params['landing_type'] : 'SHOP',
            'delivery_mode'     =>  !empty($params['delivery_mode']) ? $params['delivery_mode'] : 'MANUAL',
            'marketing_goal'    =>  !empty($params['marketing_goal']) ? $params['marketing_goal'] : 'VIDEO_AND_IMAGE',
            'ad_type'           =>  !empty($params['ad_type']) ? $params['ad_type'] : 'ALL',
            'asset_type'        =>  !empty($params['asset_type']) ? $params['asset_type'] : 'ORANGE',
        ];
        if(!empty($params['asset_id'])){
            $query['asset_id']  =   $params['asset_id'];
        }
        $url = $this->getApiUrl()['GET_OPTIMIZED_GOAL'] . '?' . http_build_query($query);
        $res = httpRequest($url,[],$header);
        return $res;    
    }

    /**
     * 获取可用深度优化方式
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_deep_bid_type($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = ['advertiser_id' =>  $advertiser_id];
        if(!empty($params['landing_type'])){
            $query['landing_type'] = $params['landing_type'];
        }
        if(!empty($params['delivery_mode'])){
            $query['delivery_mode'] = $params['delivery_mode'];
        }
        if(!empty($params['asset_id'])){
            $query['asset_id'] = $params['asset_id'];
        }
        if(!empty($params['external_action'])){
            $query['external_action'] = $params['external_action'];
        }
        if(!empty($params['deep_external_action'])){
            $query['deep_external_action'] = $params['deep_external_action'];
        }
        $url = $this->getApiUrl()['GET_DEEP_BID_TYPE'] . '?' . http_build_query($query);
        $res = httpRequest($url,[],$header);
        return $res; 
    }

    /**
     * 获取已创建资产列表
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   string  $asset_type
     */
    public function get_advertiser_assets($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
            'asset_type'    =>  $params['asset_type'],
            'page_size'     =>  isset($params['pagesize']) && is_numeric($params['pagesize']) ? $params['pagesize'] : 10,
            'page'          =>  isset($params['page']) && is_numeric($params['page']) ? $params['page'] : 1,
        ];
        $url = $this->getApiUrl()['GET_AD_ASSETS'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;     
    }

    /**
     * 获取推荐关键词
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   array   $query_list
     */
    public function get_advertiser_sugg_words($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' => (int)$advertiser_id,
        ];
        if(!empty($params['query_list'])){
            $query_list = explode(',',$params['query_list']);
            if(count($query_list) > 10){
                throw new \Exception("最多传入10个!");
            }
            $query['query_list'] = $query_list;
        }
        $url = $this->getApiUrl()['GET_SUGG_WORDS'];
        $res = httpRequest($url,json_encode($query),$header);
        return $res;    
    }

    /**
     * 获取快投推荐出价系数
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_search_bid_ratio($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
        ];
        $url = $this->getApiUrl()['GET_SEARCH_BID_RATIO'] . '?' . http_build_query($query);
        $res = httpRequest($url,[],$header);
        return $res;
    }

    /**
     * 获取抖音授权关系
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_aweme_auth_list($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  isset($params['pagesize']) && is_numeric($params['pagesize']) ? $params['pagesize'] : 10,
            'page'          =>  isset($params['page']) && is_numeric($params['page']) ? $params['page'] : 1,
        ];
        $url = $this->getApiUrl()['GET_AWEME_AUTH_LIST'] . '?' . http_build_query($query);
        $res = httpRequest($url,[],$header);
        return $res;
    }

    /**
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_creative_component_list($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  isset($params['pagesize']) && is_numeric($params['pagesize']) ? $params['pagesize'] : 10,
            'page'          =>  isset($params['page']) && is_numeric($params['page']) ? $params['page'] : 1,
        ];
        $filtering = [];
        if(!empty($params['component_types'])){
            $filtering['component_types'] = explode(',',$params['component_types']);
        }
        if(!empty($filtering)){
            $query['filtering'] = json_encode($filtering);    
        }
        $url = $this->getApiUrl()['GET_CREATIVE_COMPONENT'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;    
    }

    /**
     * 通过优化目标获取橙子落地页站点信息
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_orange_site($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  isset($params['pagesize']) && is_numeric($params['pagesize']) ? $params['pagesize'] : 10,
            'page'          =>  isset($params['page']) && is_numeric($params['page']) ? $params['page'] : 1,
        ];
        $optimize_goal = [];
        if(!empty($params['external_action'])){
            $optimize_goal['external_action'] = $params['external_action'];  
        }
        if(!empty($params['deep_external_action'])){
            $optimize_goal['deep_external_action'] = $params['deep_external_action'];    
        }
        if(!empty($optimize_goal)){
            $query['optimize_goal'] = json_encode($optimize_goal);    
        }
        $url = $this->getApiUrl()['GET_AD_ORANGE_SITE'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;   
    }

    /**
     * 获取第三方落地页站点列表
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_third_site($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  isset($params['pagesize']) && is_numeric($params['pagesize']) ? $params['pagesize'] : 10,
            'page'          =>  isset($params['page']) && is_numeric($params['page']) ? $params['page'] : 1,
        ];
        $filtering = [];
        if(!empty($params['site_id'])){
            $filtering['site_id']       = $params['site_id'];  
        }
        if(!empty($params['site_name'])){
            $filtering['site_name']     = $params['site_name'];    
        }
        if(!empty($params['start_time'])){
            $filtering['start_time']    = $params['start_time'];    
        }
        if(!empty($params['end_time'])){
            $filtering['end_time']      = $params['end_time'];    
        }
        if(!empty($filtering)){
            $query['filtering'] = json_encode($filtering);    
        }
        $url = $this->getApiUrl()['GET_THIRD_SITE'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;    
    }

    /**
     * 获取图片素材
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_material_image_list($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  isset($params['pagesize']) && is_numeric($params['pagesize']) ? $params['pagesize'] : 10,
            'page'          =>  isset($params['page']) && is_numeric($params['page']) ? $params['page'] : 1,
        ];
        $filtering = [];
        if(!empty($params['image_ids'])){
            $filtering['image_ids'] = explode(',',$params['image_ids']);  
        }
        if(!empty($params['material_ids'])){
            $filtering['material_ids'] = explode(',',$params['material_ids']);     
        }
        if(!empty($params['width'])){
            $filtering['width'] = $params['width'];    
        }
        if(!empty($params['height'])){
            $filtering['height'] = $params['height'];    
        }
        if(!empty($params['ratio'])){
            $ratio = explode(',',$params['ratio']);
            if(isset($ratio[0])){
                $filtering['ratio'][0] = (float)$ratio[0];    
            }
            if(isset($ratio[1])){
                $filtering['ratio'][1] = (float)$ratio[1];    
            }    
        }
        if(!empty($params['start_time'])){
            $filtering['start_time'] = $params['start_time'];    
        }
        if(!empty($params['end_time'])){
            $filtering['end_time'] = $params['end_time'];    
        }
        if(!empty($filtering)){
            $query['filtering'] = json_encode($filtering);    
        }
        $url = $this->getApiUrl()['GET_IMAGE_MATERIAL'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;
    }

    /**
     * 获取视频素材
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_material_video_list($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
            'page_size'     =>  isset($params['pagesize']) && is_numeric($params['pagesize']) ? $params['pagesize'] : 10,
            'page'          =>  isset($params['page']) && is_numeric($params['page']) ? $params['page'] : 1,
        ];
        $filtering = [];
        if(!empty($params['video_ids'])){
            $filtering['video_ids']     = explode(',',$params['video_ids']);  
        }
        if(!empty($params['material_ids'])){
            $filtering['material_ids']  = explode(',',$params['material_ids']);     
        }
        if(!empty($params['width'])){
            $filtering['width']         = $params['width'];    
        }
        if(!empty($params['height'])){
            $filtering['height']        = $params['height'];    
        }
        if(!empty($params['ratio'])){
            $ratio = explode(',',$params['ratio']);
            if(isset($ratio[0])){
                $filtering['ratio'][0] = (float)$ratio[0];    
            }
            if(isset($ratio[1])){
                $filtering['ratio'][1] = (float)$ratio[1];    
            }    
        }
        if(!empty($params['start_time'])){
            $filtering['start_time']    = $params['start_time'];    
        }
        if(!empty($params['end_time'])){
            $filtering['end_time']      = $params['end_time'];    
        }
        if(!empty($params['labels'])){
            $filtering['labels']        = explode(',',$params['labels']);     
        }
        if(!empty($params['source'])){
            $filtering['source']        = explode(',',$params['source']);     
        }
        if(!empty($filtering)){
            $query['filtering'] = json_encode($filtering);    
        }
        $url = $this->getApiUrl()['GET_VIDEO_MATERIAL'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;    
    }

    /**
     * 获取关联云图的广告主账户信息
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_advertiser_brand_info($access_token, $advertiser_id)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
        ];
        $url = $this->getApiUrl()['GET_AD_BRAND_INFO'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;    
    }

    /**
     * 查询推广卡片推荐内容（新版）
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   array   $params
     */
    public function get_advertiser_recommend_title($access_token, $advertiser_id, $params)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
        ];
        if(!empty($params['text_type'])){
            $query['text_type'] = $params['text_type'];    
        }
        $url = $this->getApiUrl()['GET_RECOMMEND_TITLE'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;     
    }

    /**
     * 获取广告主消耗数据
     * @param   string  $access_token
     * @param   string  $advertiser_id
     * @param   string  $start
     * @param   string  $end
     */
    public function get_advertiser_report($access_token, $advertiser_id, $start, $end)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_id' =>  $advertiser_id,
            'start_date'    =>  $start,
            'end_date'      =>  $end,
            'fields'        =>  json_encode($this->ad_report_fields),
        ];
        $url = $this->getApiUrl()['GET_ADVERTISER_REPORT'] . '?' . http_build_query($query);
        $res = httpRequestBigint($url,[],$header);
        return $res;    
    }

    /**
     * 获取千川广告账户基础信息
     * @param   string  $access_token
     * @param   string  $advertiser_id
     */
    public function get_qianchuan_advertiser_info($access_token, $advertiser_id)
    {
        $header = array_merge($this->common_header,[
            "Access-Token: ".$access_token,
        ]);
        $query = [
            'advertiser_ids'     =>  json_encode([
                $advertiser_id    
            ])
        ];
        $url = $this->getApiUrl()['ADVERTISER_PUBLIC_INFO'] . '?' . http_build_query($query);
        $res = httpRequest($url,[],$header);
        return $res;
    }
}